<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="关键字" prop="searchParam">
        <el-input
            v-model="queryParams.searchParam"
            placeholder="请输入订单号、用户ID、手机号搜索"
            clearable
            size="small"
            style="width: 200px"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充值时间" prop="source">
        <el-date-picker
          v-model="dateRange"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="handleDateChange">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否到期" prop="expire">
        <el-select
            v-model="queryParams.expire"
            placeholder="是否到期"
            clearable
            size="small"
            style="width: 200px"
        >
          <el-option
              v-for="dict in expireType"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="来源渠道" prop="channel">
        <el-select
            v-model="queryParams.channel"
            placeholder="来源渠道"
            clearable
            size="small"
            style="width: 200px"
        >
          <el-option
              v-for="dict in dict.type.channel"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="default"
            size="mini"
            icon="el-icon-delete"
            @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" prop="id" align="center" width="120" sortable />
      <el-table-column label="订单号" align="center" prop="orderNum" :show-overflow-tooltip="true" width="160" />
      <el-table-column label="用户ID" align="center" prop="userId" :show-overflow-tooltip="true" width="120" />
      <el-table-column label="用户名" align="center" prop="username" width="160"/>
      <el-table-column label="来源渠道" prop="channel" align="center" width="100"/>
      <el-table-column label="支付金额" prop="payAmount" align="center" width="100">
        <template slot-scope="scope">
          <span>￥{{ scope.row.payAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充值时间" align="center" prop="payDate" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.payDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到期时间" align="center" prop="expireDate" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.expireDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="payType" width="100"/>
      <el-table-column label="有效期" align="center" prop="validityPeriod" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.validityPeriod }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否到期" align="center" prop="expire" width="120">
        <template slot-scope="scope">
          <span :style="scope.row.expire === '已到期' ? 'color: #f00' : ''">{{ scope.row.expire }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script>
import { getUserOrderList } from "@/api/shuiyin/shuiyin";
import {removeUserOrder} from "../../api/shuiyin/shuiyin";

export default {
  name: "UserMemberOrder",
  dicts: ['channel'],
  data() {
    return {
      expireType: [
        {
          label: '已到期',
          value: true
        },
        {
          label: '未到期',
          value: false
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      list: [],
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        expire: undefined,
        payDateBeginDate: undefined,
        payDateEndDate: undefined,
        searchParam: undefined,
        channel: null
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      getUserOrderList(this.queryParams).then(
        response => {
          this.list = response.data.rows;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 日期时间选择组件确定事件
    handleDateChange(event) {
      console.log(event);
      this.queryParams.payDateBeginDate = event[0];
      this.queryParams.payDateEndDate = event[1];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.payDateBeginDate = undefined;
      this.queryParams.payDateEndDate = undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roleId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 分配用户操作 */
    handleDetail: function(row) {
      const orderId = row.id;
      this.$router.push("/orderDetail/order/" + orderId);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + orderIds + '"的数据项？').then(function() {
        return removeUserOrder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
